package com.voxel.sense.infomsg.module.firm.search;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.common.VRecyclerFrag;
import com.voxel.sense.infomsg.module.firm.FirmListInfoActivity;
import com.voxel.sense.infomsg.module.firm.FirmListInfoAdapter;
import com.voxel.sense.infomsg.module.firm.details.FirmInfoDetailActivity;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;

import butterknife.BindView;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.FirmListEntity;

/**
 * 公司列表fragment
 * Created by ChenYiYao on 2018/8/7.
 */
public class SearchFirmFrag extends VRecyclerFrag<SearchFirmContract.Presenter, BaseEntity>
        implements SearchFirmContract.View<BaseEntity> {
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    private String mSearchConditon;

    private RecyclerAdapter mAdapter;


    @Override
    protected int getContentLayoutId() {
        return R.layout.layout_recycler_emptty;
    }

    @Override
    protected SearchFirmContract.Presenter initPresenter() {
        return new SearchFirmPresenter(this);
    }

    @Override
    protected void initData() {
        super.initData();
        //初始化recycleView
        recycle_view.setLayoutManager(new LinearLayoutManager(getContext()));
        //设置下划线
        LinearItemDecoration linearItemDecoration = new LinearItemDecoration(R.drawable.recycle_divider_10dp);
        linearItemDecoration.setDrawFirstItem(true).setDrawVerticalModelLeftAndRightBorder(true,true);
        recycle_view.addItemDecoration(linearItemDecoration);
        //初始化适配器
        mAdapter = new FirmListInfoAdapter(new RecyclerAdapter.AdapterListenerImpl<FirmListEntity>() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, FirmListEntity bean) {
                super.onItemClick(holder, bean);

                FirmInfoDetailActivity.show(getContext(), bean, bean.getCompanyId());

            }
        });
        recycle_view.setAdapter(mAdapter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        refresh_layout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.doSearch(mSearchConditon);
                //重新刷新，则需要改变没有更多的状态
                refresh_layout.setNoMoreData(false);
            }

            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                mPresenter.doLoadMoreData();
            }
        });
    }

    @Override
    public void onUiRefresh(String conditon) {
        this.mSearchConditon = conditon;
        //拉起刷新ui事件
        empty_view.triggerOk();
        refresh_layout.setEnableRefresh(true);
        refresh_layout.autoRefresh();
    }

    @Override
    public RecyclerAdapter<BaseEntity> getRecyclerViewAdapter() {
        return mAdapter;
    }

    @Override
    public void onRefreshFinish() {
        super.onRefreshFinish();
        refresh_layout.setEnableRefresh(false);
    }


}
